package com.community.zhihuishequ.third;

import android.app.Application;
import android.content.Context;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.view.SurfaceView;

import com.community.zhihuishequ.BuildConfig;
import com.community.zhihuishequ.component.CommunityApplication;
import com.community.zhihuishequ.utils.TextUtil;
import com.hik.mcrsdk.MCRSDK;
import com.hik.mcrsdk.rtsp.RtspClient;
import com.hik.mcrsdk.talk.TalkClientSDK;
import com.hikvision.sdk.VMSNetSDK;
import com.hikvision.sdk.consts.SDKConstant;
import com.hikvision.sdk.net.bean.SubResourceNodeBean;
import com.hikvision.sdk.net.business.OnVMSNetSDKBusiness;

/**
 * Created by Rick on 17/11/21.
 * 海康SDK的调用接口
 */

public class HikInterface {
    public final static int HD = SDKConstant.LiveSDKConstant.MAIN_HIGH_STREAM;      // 高清
    public final static int SD = SDKConstant.LiveSDKConstant.SUB_STREAM;            // 标清
    public final static int LD = SDKConstant.LiveSDKConstant.SUB_STANDARD_STREAM;   // 低清

    private final static String URL = BuildConfig.HIK_8700_SERVER;
    private final static String USER_NAME = BuildConfig.HIK_8700_USERNAME;
    private final static String PASSWORD = BuildConfig.HIK_8700_PASSWORD;


    private static boolean init = false;

    public static void init() {
        if (init) {
            return;
        }
        init = true;

        MCRSDK.init();
        // 初始化RTSP
        RtspClient.initLib();
        MCRSDK.setPrint(1, null);
        // 初始化语音对讲
        TalkClientSDK.initLib();
        // SDK初始化
        VMSNetSDK.init((Application) CommunityApplication.getContext());

    }

    public static void unInit() {
        if (!init) {
            return;
        }
        init = false;
        TalkClientSDK.finiLib();
        RtspClient.finiLib();
        MCRSDK.fini();
    }

    /***
     * 登录方法
     */
    public static void login(OnVMSNetSDKBusiness listener) {
        String macAddress = getMacAddress();
        if (TextUtil.isStrEmpty(macAddress)) {
            return;
        }

        VMSNetSDK.getInstance().Login(URL, USER_NAME, PASSWORD, macAddress, listener);
    }

    /***
     * 退出登录
     */
    public static void logout(OnVMSNetSDKBusiness listener) {
        VMSNetSDK.getInstance().Logout(listener);
    }

    /**
     * 获取控制中心根部分资源
     */
    public static void getRootControlCenter(OnVMSNetSDKBusiness listener) {
        VMSNetSDK.getInstance().getRootCtrlCenterInfo(1, SDKConstant.SysType.TYPE_VIDEO, 999, listener);
    }

    /**
     * 获取父节点资源列表
     */
    public static void getSubResourceList(int parentNodeType, int pId, OnVMSNetSDKBusiness listener) {
        VMSNetSDK.getInstance().getSubResourceList(1, 999, SDKConstant.SysType.TYPE_VIDEO, parentNodeType, String.valueOf(pId), listener);
    }

    /**
     * 检查该节点是否具备权限
     */
    public static boolean checkLivePermission(SubResourceNodeBean node) {
        return VMSNetSDK.getInstance().checkLivePermission(node);
    }

    /**
     * 开始直播
     *
     * @param streamType 参考 SDKConstant.LiveSDKConstant.MAIN_HIGH_STREAM
     */
    public static void startLive(int windowId, SubResourceNodeBean cameraData, SurfaceView surfaceView, int streamType, OnVMSNetSDKBusiness listener) {
        if (!VMSNetSDK.getInstance().checkLivePermission(cameraData)) {
            // TODO 没有权限，怎样提示
            return;
        }
        VMSNetSDK.getInstance().startLiveOpt(windowId, cameraData.getSysCode(), surfaceView, streamType, listener);
    }

    /**
     * 暂停直播
     */
    public static void stopLive(int windowId) {
        boolean stopLiveResult = VMSNetSDK.getInstance().stopLiveOpt(windowId);
    }


    /**
     * 获取登录设备mac地址
     *
     * @return Mac地址
     */
    private static String getMacAddress() {
        WifiManager wm = (WifiManager) CommunityApplication.getContext().getApplicationContext().getSystemService(Context.WIFI_SERVICE);
        WifiInfo connectionInfo = wm != null ? wm.getConnectionInfo() : null;
        String mac = connectionInfo != null ? connectionInfo.getMacAddress() : null;
        return mac == null ? "" : mac;
    }
}
